
SUBROUTINE fmm_TIMTXT(TEXTIN,TIMUSD,IUNIT)

! fmm_TIMTXT based on TIMER by TUH //900709-hjaaj

   USE fmm_global_types
   USE fmm_global_consts
   IMPLICIT NONE
   CHARACTER(*) :: TEXTIN
   CHARACTER :: AHOUR*6, ASEC*8, AMIN*8
   REAL(REALK) :: TIMUSD
   INTEGER :: i, ISECND, IUNIT, IHOURS, MINUTE
   CHARACTER(40) :: TEXT
  
   TEXT = TEXTIN

   ISECND = INT(TIMUSD)
!   IF (ISECND .GE. 60) THEN
   IF (.FALSE.) THEN
      MINUTE = ISECND/60
      IHOURS = MINUTE/60
      MINUTE = MINUTE - 60*IHOURS
      ISECND = ISECND - 3600*IHOURS - 60*MINUTE
      IF (IHOURS .EQ. 1) THEN
         AHOUR = ' hour '
      ELSE
         AHOUR = ' hours'
      END IF
      IF (MINUTE .EQ. 1) THEN
         AMIN = ' minute '
      ELSE
         AMIN = ' minutes'
      END IF
      IF (ISECND .EQ. 1) THEN
         ASEC = ' second '
      ELSE
         ASEC = ' seconds'
      END IF
      IF (IHOURS .GT. 0) THEN
         WRITE(IUNIT,100) TEXT, IHOURS, AHOUR, MINUTE, AMIN, ISECND, ASEC
      ELSE
         WRITE(IUNIT,200) TEXT, MINUTE, AMIN, ISECND, ASEC
      END IF
   ELSE
      WRITE(IUNIT,300) TEXT,TIMUSD
   END IF
100 FORMAT(1X,A,I4,A,I3,A,I3,A)
200 FORMAT(1X,A,     I3,A,I3,A)
300 FORMAT(1X,A,F12.2,' seconds')
   RETURN
END SUBROUTINE fmm_TIMTXT

!-------------------------------------------------------------------------------

SUBROUTINE fmm_quit(msg)
   CHARACTER(*) msg
   print *, msg
   STOP ">>> FATAL ERROR"
END SUBROUTINE fmm_quit

!------------------------------------------------------------------------------

SUBROUTINE fmm_matrix_norm(label,matrix,ndim)

   USE fmm_global_types
   USE fmm_global_consts
   IMPLICIT NONE
   CHARACTER(*), INTENT(IN) :: label
   INTEGER,      INTENT(IN) :: ndim
   REAL(REALK),  INTENT(IN) :: matrix(ndim)

   INTEGER :: i
   REAL(REALK) :: norm

   norm = 0d0
   DO i = 1, ndim
      norm = norm + matrix(i)*matrix(i)
   END DO
   WRITE(*,*) 'o fmm_matrix_norm: ', label,' = ', SQRT(norm)

END SUBROUTINE fmm_matrix_norm

!------------------------------------------------------------------------------

MODULE fmm_utils

   USE fmm_global_types
   USE fmm_global_consts
   IMPLICIT NONE
   PRIVATE
   ! Public procedures
   PUBLIC :: fmm_distance_between_lines,   &
             fmm_loop_progress

CONTAINS

!-------------------------------------------------------------------------------

   FUNCTION fmm_cross_product(a,b)

      IMPLICIT NONE
      REAL(REALK), INTENT(IN)  :: a(3), b(3)
      REAL(REALK) :: fmm_cross_product(3)

      fmm_cross_product(1) =    a(2)*b(3) - a(3)*b(2) 
      fmm_cross_product(2) = -( a(1)*b(3) - a(3)*b(1) )
      fmm_cross_product(3) =    a(1)*b(2) - a(2)*b(1)

   END FUNCTION fmm_cross_product

!-------------------------------------------------------------------------------

   FUNCTION fmm_distance_between_lines(p,q,ep,eq)

      IMPLICIT NONE
      REAL(REALK), INTENT(IN)  :: p(3), q(3), ep(3), eq(3)
      REAL(REALK) :: fmm_distance_between_lines

      REAL(REALK) :: d1, d2, n(3)

      n = fmm_cross_product(ep,eq) 

      d1 = DOT_PRODUCT(p,n)
      d2 = DOT_PRODUCT(q,n)

      fmm_distance_between_lines = ABS(d1 - d2)

   END FUNCTION fmm_distance_between_lines

!-------------------------------------------------------------------------------

   SUBROUTINE fmm_loop_progress(i,n)

      IMPLICIT NONE
      INTEGER(INTK), INTENT(IN) :: i, n

      IF (n > 10) THEN
         IF (MOD(i,n/10)==0) print *, CEILING(100d0*i/n),'%'
      END IF

   END SUBROUTINE fmm_loop_progress

!-------------------------------------------------------------------------------

END MODULE fmm_utils

